package org.swingDemo.swingWorker;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.swingDemo.UI.MainFrame;
import org.swingDemo.helper.ClassifyOptionHelper;
import org.swingDemo.helper.FileHelper;
import org.swingDemo.helper.StringHelper;

import javax.swing.*;
import java.io.File;
import java.util.List;

public class FileClassifyWorker extends SwingWorker<Void, Void> {

    private String sourcePath;
    private String targetPath;

    private final String START_MOVE_FORMAT = "开始移动文件 [{}] 至 [{}]";
    private final String FINISH_MOVE_FORMAT = " -- 成功移动文件 [{}]";

    public FileClassifyWorker(String sourcePath, String targetPath) {
        this.sourcePath = sourcePath;
        this.targetPath = targetPath;

        addPropertyChangeListener(MainFrame.mainFrame);
    }

    @Override
    protected Void doInBackground() throws Exception {
        try {
            // 获取源目录及其子目录中的所有文件列表
            List<File> fileList = FileHelper.getInstance().getFileList(sourcePath);
            firePropertyChange(StringHelper.PROPERTY_FILE_COUNT, 0, fileList.size());

            int index = 0;
            for (File file : fileList) {
                firePropertyChange(StringHelper.PROPERTY_FILE_INDEX, index, ++index);

                String parentPath = file.getParent();
                String fileName = file.getName();

                // 文件应该移动到的新文件夹名称
                String folderName = FileHelper.getInstance().getNewFolderOfFile(fileName);
                if (folderName.isEmpty()) {
                    continue;
                }

                // 判断是否需要保留源目录结构
                if (ClassifyOptionHelper.isPersistSubDir()) {
                    folderName = parentPath.replace(sourcePath, targetPath + File.separator + folderName);
                } else {
                    folderName = targetPath + File.separator + folderName;
                }
                // 判断文件夹是否存在目的目录中，如果不存在，则新建文件夹
                firePropertyChange(StringHelper.PROPERTY_INFO, "",
                        StrUtil.format(START_MOVE_FORMAT, file.getAbsolutePath(), folderName));

                File folderFile = null;
                if (!FileUtil.exist(folderName) || !FileUtil.isDirectory(folderName)) {
                    folderFile = FileUtil.mkdir(folderName);
                } else {
                    folderFile = FileUtil.file(folderName);
                }

                // 将文件移动到符合条件的新目录中
                if (ClassifyOptionHelper.isCopyOrMove()) {
                    FileUtil.copy(file, folderFile, true);
                } else {
                    FileUtil.move(file, folderFile, true);
                }

                firePropertyChange(StringHelper.PROPERTY_INFO, "",
                        StrUtil.format(FINISH_MOVE_FORMAT, file.getAbsolutePath()));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            firePropertyChange(StringHelper.PROPERTY_ERROR, "",
                    "移动文件失败: " + ex);
        }
        return null;
    }

    @Override
    protected void done() {
        MainFrame.mainFrame.onFileClassifyWorkerDone();
    }
}
